package com.everydayflash.flar.sample.pv3d {
	import org.libspark.flartoolkit.core.FLARCode;
	import org.libspark.flartoolkit.core.param.FLARParam;
	import org.libspark.flartoolkit.core.raster.rgb.FLARRgbRaster_BitmapData;
	import org.libspark.flartoolkit.detector.FLARSingleMarkerDetector;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.PixelSnapping;
	import flash.display.Sprite;
	import flash.media.Camera;
	import flash.media.Video;
	import flash.utils.ByteArray;	

	[Event(name="init",type="flash.events.Event")]

	[Event(name="init",type="flash.events.Event")]

	[Event(name="ioError",type="flash.events.IOErrorEvent")]

	[Event(name="securityError",type="flash.events.SecurityErrorEvent")]

	public class ARAppBase extends Sprite {

		private var _width:int;
		private var _height:int;
		private var _codeWidth:int;

		protected var _param:FLARParam;
		protected var _code:FLARCode;
		protected var _raster:FLARRgbRaster_BitmapData;
		protected var _detector:FLARSingleMarkerDetector;

		protected var _webcam:Camera;
		protected var _video:Video;
		protected var _capture:Bitmap;

		
		public function ARAppBase() {
		}

		protected function init(cameraFile:ByteArray, codeFile:ByteArray, canvasWidth:int, canvasHeight:int, codeWidth:int = 80):void {
			_width = canvasWidth;
			_height = canvasHeight;
			_codeWidth = codeWidth;

			_param = new FLARParam();
			_param.loadARParam(cameraFile);
			_param.changeScreenSize(_width, _height);

			_code = new FLARCode(16, 16);
			_code.loadARPatt(codeFile.toString());

			// setup webcam
			_webcam = Camera.getCamera();
			
			if (!_webcam) throw new Error("No webcam!");

			_webcam.setMode(_width, _height, 15);
			_video = new Video(_width, _height);
			_video.attachCamera(_webcam);
			_capture = new Bitmap(new BitmapData(_width, _height, false, 0), PixelSnapping.AUTO, true);
			
			// setup ARToolkit
			_raster = new FLARRgbRaster_BitmapData(_capture.bitmapData);
			_detector = new FLARSingleMarkerDetector(_param, _code, _codeWidth);
		}
	}
}